class Solution:
    def countBattleships(self, board: list) -> int:
        res = 0
        length, height = len(board[0]), len(board)
        for i in range(height):
            for j in range(length):
                if board[i][j] == "X" and (i == 0 or i > 0 and board[i-1][j] != "X") and \
                        (j == 0 or j > 0 and board[i][j-1] != "X"):
                    res += 1
        return res


class Solution:
    def countBattleships(self, board: list) -> int:
        res = 0
        length, height = len(board[0]), len(board)
        for i in range(height):
            for j in range(length):
                if board[i][j] == "X":
                    res += 1
                    if i < height - 1 and board[i+1][j] == "X":
                        mid = i
                        while mid < height and board[mid][j] == "X":
                            board[mid][j] = "O"
                            mid += 1
                    elif j < length - 1 and board[i][j+1] == "X":
                        mid = j
                        while mid < length and board[i][mid] == "X":
                            board[i][mid] = "O"
                            mid += 1
        return res


a = Solution()
print(a.countBattleships([["X", ".", ".", "X"], [".", ".", ".", "X"], [".", ".", ".", "X"]]))
